package com.demo.cms.action.manage;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.User;
import com.demo.core.service.base.UserService;
import com.demo.core.service.exception.UserAlreadyExistedException;
import com.demo.security.annotation.function.Function;
import com.demo.webapp.action.PageAction;

/**
 * 用户管理ACTION
 * @author ryuu.kk
 *
 */
@Function(id = 100600, pid = 10, code = "manage.user")
public class UserAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 7961235256996422808L;

	/**
	 * 用户ID
	 */
	private Long id;
	
	/**
	 * 再次输入的密码
	 */
	private String repass;
	
	/**
	 * 用户模型
	 */
	private User user;
	
	/**
	 * 用户模型列表
	 */
	private List<User> userList;
	
	/**
	 * 用户服务
	 */
	private UserService userService;

	/**
	 * 密码编码对象
	 */
	private PasswordEncoder passwordEncoder;
	/**
	 * 用户一览
	 * @return
	 */
	@Function(id = 100610, pid = 100600, code = "manage.user.list")
	@Transactional
	public String list() {
		
		// 取得分页参数模型
		SearchArgumentSupport args = getArgumentSupport();
		ResultSearchResult<User> result = userService.search(args);
		this.setResultSupport(result);
		userList = new ArrayList<User>(result.getResults());
		return SUCCESS;
	}
	
	/**
	 * 用户创建画面
	 * @return String
	 */
	@Function(id = 100620, pid = 100600, code = "manage.user.create")
	public String create() {
		return SUCCESS;
	}
	
	/**
	 * 创建频道信息
	 * @return String
	 */
	@Function(id = 100630, pid = 100600, code = "manage.user.save")
	public String save() {
		// 取得站点ID
		//Site site = MultisiteContextHolder.getSite();
		//Long siteId = site.getId();
		// 账户可用
		user.setEnabled(true);
		// 账户未锁
		user.setLocked(false);
		// 账户有效
		user.setIsValid(true);
		// 密码盐值
		user.setSalt("");
		user.setRegisterTime(new Date());
		// 有效期限日
		if (user.getExpireDay() == null) {
			Calendar calendar = Calendar.getInstance();
			// 用户有效期限日到2014-12-31
			calendar.set(2014, 12, 31, 0, 0);
			//SimpleDateFormat.getDateInstance().getCalendar();
			user.setExpireDay(calendar.getTime());
		}
		// 用户密码更新
		String password = passwordEncoder.encodePassword(user.getPassword(), user.getSalt());
		user.setPassword(password);
		try {
			id = userService.save(user);
		} catch (UserAlreadyExistedException e) {
			// 用户存在
			addActionError(getText(e.getI18n(),"", user.getUserName()));
			return INPUT;
		}
		addActionMessage(getText("user.added", "", user.getUserName()));
		return CALLBACK;
	}
	
	/**
	 * 用户编辑画面
	 * @return String 
	 */
	@Function(id = 100640, pid = 100600, code = "manage.user.edit")
	public String edit() {
		
		// 查找用户
		user = userService.find(id);
		// 查找机构
		// ...
		return SUCCESS;
	}
	/**
	 * 用户更新画面
	 * @return String
	 */
	@Function(id = 100650, pid = 100600, code = "manage.user.update")
	public String update() {
		
		// 获取用户
		User userTmp = userService.find(id);
		if (userTmp == null) {
			addActionMessage(getText("user.notfound"));
			return INPUT;
		}
		// 用户密码更新(加密)
		//user.setPassword(password);
		if (!userTmp.getPassword().equals(user.getPassword())) {
			userService.resetPassword(id, user.getPassword());
		}
		// 更新用户信息
		userService.update(id, user);
		
		addActionMessage(getText("user.updated", "", userTmp.getUserName()));
		return CALLBACK;
	}
	
	/**
	 * 删除用户信息
	 * @return String
	 */
	@Function(id = 100660, pid = 100600, code = "manage.user.delete")
	public String delete() {
		userService.delete(id);
		addActionMessage("用户信息删除成功.");
		return CALLBACK;
	}
	
	/**
	 * 用户可用
	 * @return String
	 */
	@Function(id = 100670, pid = 100600, code = "manage.user.enabled")
	public String enabled() {
		userService.enable(id, user.getEnabled());
		return CALLBACK;
	}
	
	/**
	 * 用户锁定
	 * @return String 
	 */
	@Function(id = 100671, pid = 100600, code = "manage.user.locked")
	public String locked() {
		userService.lock(id, user.getLocked());
		return CALLBACK;
	}
	
	/**
	 * 用户有效
	 * @return String
	 */
	@Function(id = 100672, pid = 100600, code = "manage.user.isvalid")
	public String isvalid() {
		userService.isValid(id, user.getIsValid());
		return CALLBACK;
	}
	
	/**
	 * 用户有效期限日
	 * @return String
	 */
	@Function(id = 100673, pid = 100600, code = "manage.user.expireday")
	public String expireday() {
		userService.expireDay(id, user.getExpireDay());
		return CALLBACK;
	}
	
	/**
	 * 批量删除用户信息
	 * @return String
	 */
	@Function(id = 100680, pid = 100600, code = "manage.user.rm")
	public String rm() {
		if (userList != null) {
			for (User user : userList) {
				userService.delete(user.getId());
			}
		}
		addActionMessage("用户信息删除成功.");
		return CALLBACK;
	}
	
	// ~~ settings here ------------------------------------------------------------
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getRepass() {
		return repass;
	}

	public void setRepass(String repass) {
		this.repass = repass;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}
}
